/*
 * Copyright (C) 2010 Juan José Ortilles jortilles@gmail.com
 *
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation; either version 3 of the License, or 
 * any later version.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 *
 */

package net.juantxu.pentaho.launcher.ejecutores;

import java.io.File;
import java.util.Properties;

import net.juantxu.pentaho.launcher.utilidades.CargaProperties;
import net.juantxu.pentaho.launcher.utilidades.ValidaEjecutableNix;
import net.juantxu.pentaho.launcher.utilidades.ValidaEjecutableWin;

import org.apache.log4j.Logger;

public class EjecutaWorkbench implements EjecutaPentaho {
	static Logger log = Logger.getLogger(EjecutaPentaho.class);
	
	public int lanza(){
		int resultado = 0;
		String os = "";
		log.debug("lanzando el wokbench");
		os =System.getProperty("os.name");
		log.debug("sistema operativo :".concat(os));	
		if (os.toLowerCase().contains("win")){
			resultado = ejecutaWin();
		}else{
			log.debug("es un sistema *NIX");
			resultado = ejecutaNix();
		}
		return resultado;
	}

	public int ejecutaWin(){
		int resultado = 0;
		Properties prop = new CargaProperties().Carga();
		File miPath = new File( prop.getProperty("workbenchRelativePath"));
		log.debug("Ejecutando en :" + miPath.getAbsolutePath());
		if (  new ValidaEjecutableWin().valida(miPath.getAbsolutePath()+"\\workbench.bat") ){
		Runtime r = Runtime.getRuntime();
		try {
			log.debug("ejecutando como en nix");
			r.exec(miPath.getAbsolutePath()+"\\workbench.bat", null, miPath );
			 Thread.sleep(3000);
		} catch (Exception e) {
			e.printStackTrace();
			resultado = 1;
		}
		}else{
			//no se encuetra el ejecutable
			log.error("no se encuentra el ejecutable");
			resultado=7;
		}
		return resultado;
	}
	public int ejecutaNix(){
		int resultado = 0;
		Properties prop = new CargaProperties().Carga();
		
		
		if (new ValidaEjecutableNix().valida(prop.getProperty("workbenchRelativePath")+"/workbench.sh"))
		{
		File miPath = new File( prop.getProperty("workbenchRelativePath"));
		log.debug("Ejecutando en :" + miPath.getAbsolutePath());
		Runtime r = Runtime.getRuntime();
		try {
			r.exec("./workbench.sh", null, miPath );
			// dandole algún tiempo para arrancar
			 Thread.sleep(3000);
		} catch (Exception e) {
			resultado =1;
			e.printStackTrace();
		}
		}else{
			//no se encuetra el ejecutable
			log.error("no se encuentra el ejecutable");
			resultado=7;
		}
		return resultado;
	}
}
